package net.liteheaven.xblib.widget.hscroll;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

/**
 * Created by liangy on 2016/7/22.
 * 横向滚动指示器
 */
public class HScrollIndicator extends LinearLayout{

    private HorizontalScrollView mScroll;
    private LinearLayout mContentView;
    private View [] mViews;

    public HScrollIndicator(Context context) {
        super(context);
        init();
    }

    public HScrollIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HScrollIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mContentView = new LinearLayout(getContext());
        mContentView.setOrientation(HORIZONTAL);
        mScroll = new HorizontalScrollView(getContext());
        mScroll.setHorizontalScrollBarEnabled(false);
        mScroll.addView(mContentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        addView(mScroll);
    }

    public void initTabs(View [] views){
        mViews = views;
        mContentView.removeAllViews();
        for (int i = 0; i<views.length; i++){
            mContentView.addView(views[i]);
        }
        selectTab(0);
    }

    public void selectTab(int pageIndex){
        View v = mViews[pageIndex];
        for (int i = 0; i<mContentView.getChildCount(); i++){
            View child = mContentView.getChildAt(i);
            child.setSelected(false);
        }
        v.setSelected(true);
        int left = v.getLeft();
        mScroll.smoothScrollTo(left, 0);
    }
}
